import numpy as np
import pickle,base64,scipy
import datetime

def get_data(street, db):
	try:
		sql='select data from street_complete where street='+street+';'
		print sql
		r = db.plain(sql)
		data = r[0][0]
		data = pickle.loads(base64.b64decode(data))
		return data
	except:
		return None

def get_forecast(street, db):
	sql='select xtime from forecast where street='+street+' order by xtime;'
	r = db.plain(sql)
	tdata = []
	for tick in r:
		tdata.append(tick[0])
	return tdata

def fetch():
	mysqllib = __import__('mysqllib')
	db = mysqllib.DB()
	#sql = 'select distinct street from forecast where velocity=-1 order by rand() limit 10;'
	sql = 'select distinct street from forecast where velocity=-1;'
	r = db.plain(sql)

	streets = []
	for st in r:
		street = str(st[0])
		d = {}
		d['street'] = street
		d['data'] = get_data(street, db)
		d['forecast'] = get_forecast(street, db)
		if d['data']:
			d = pickle.dumps(d)
			streets.append(d)
	return streets
